﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ThuPhap.Models;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.Linq;

namespace ThuPhap.Class
{
    public class SaveGalleryData
    {
        private List<GalleryItem> gallery = new List<GalleryItem>();
        public List<GalleryItem> Gallery
        {
            get { return gallery; }
            set { gallery = value; }
        }


        /// <summary>
        /// Store data of favorite into isolatedStorage
        /// </summary>
        /// <param name="Flower">GalleryItem Object</param>
        ///  <param name="Type">String value which is type of file.(favorite or history)</param>
        public void AddGalleryItem(GalleryItem Gallery, string Type = "gallery")
        {
            SaveGalleryData gallery = new SaveGalleryData();
            string filename = "Celligraphy\\" + Type + ".xml";
            var storage = IsolatedStorageFile.GetUserStoreForApplication();

            if (storage.DirectoryExists("Celligraphy") == false)//directory does not exist
            {
                //create Celligraphy directory
                storage.CreateDirectory("Celligraphy");
            }

            if (storage.FileExists(filename))//file already exists
            {
                //Retrieve data from current favorite.xml
                gallery = this.RetrieveData(Type);
                //Delete favorite.xml file
                storage.DeleteFile(filename);
            }

            gallery.Gallery.Add(Gallery);

            using (var storageFile = storage.CreateFile(filename))
            {
                var xmlSerializer = new XmlSerializer(typeof(SaveGalleryData));
                xmlSerializer.Serialize(storageFile, gallery);
            }
        }

        /// <summary>
        /// Remove a flower in favorites list
        /// </summary>
        /// <param name="Flower">Flower object</param>
        ///  <param name="Type">String value which is type of file.(favorite or history)</param>
        public void RemoveGalleryItem(GalleryItem Gallery, string Type = "gallery")
        {
            string filename = "Celligraphy\\" + Type + ".xml";
            SaveGalleryData gallery = new SaveGalleryData();
            var storage = IsolatedStorageFile.GetUserStoreForApplication();

            if (storage.DirectoryExists("Celligraphy") == false)//directory does not exist
            {
                //create Celligraphy directory
                storage.CreateDirectory("Celligraphy");
            }

            if (storage.FileExists(filename))//file already exists
            {
                //Retrieve data from current favorite.xml
                gallery = this.RetrieveData(Type);
                //Delete favorite.xml file
                storage.DeleteFile(filename);
            }

            GalleryItem deletedFlower = gallery.Gallery.Single(o => o.Name == Gallery.Name);

            gallery.Gallery.Remove(deletedFlower);
            using (var storageFile = storage.CreateFile(filename))
            {
                var xmlSerializer = new XmlSerializer(typeof(SaveGalleryData));
                xmlSerializer.Serialize(storageFile, gallery);
            }
        }

        /// <summary>
        /// Get data from favorite.xml or history file.
        /// Return object whose type is SaveData object(which have a GalleryItem list). Return NULL if no data
        /// </summary>
        /// <param name="Type">String value which is type of file.(favorite or history)</param>
        /// <returns>Favorite Object. NULL if no data</returns>
        public SaveGalleryData RetrieveData(string Type = "gallery")
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string filename = "Celligraphy\\" + Type + ".xml";
            if (storage.DirectoryExists("Celligraphy") && storage.FileExists(filename))
            {
                using (var storageFile = storage.OpenFile(filename, System.IO.FileMode.Open))
                {
                    var xmlSerializer = new XmlSerializer(typeof(SaveGalleryData));

                    return (SaveGalleryData)xmlSerializer.Deserialize(storageFile);
                }
            }

            return null;
        }

        /// <summary>
        /// Check flower in Favorite Flowers list
        /// </summary>
        /// <param name="Flower">GalleryItem object</param>
        /// <param name="Type">String value which is type of file.(favorite or history)</param>
        /// <returns></returns>
        public bool CheckGalleryItem(GalleryItem Gallery, string Type = "gallery")
        {
            var data = this.RetrieveData(Type);

            if (data == null)
                return false;
            else
            {
                foreach (var item in data.Gallery)
                {
                    if (item.Name == Gallery.Name)
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Delete all item in storage file
        /// </summary>
        /// <param name="Type">String value which is type of file.(favorite or history)</param>
        /// <returns></returns>
        public bool DeleteAllFlowerItem(string Type = "gallery")
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string filename = "Celligraphy\\" + Type + ".xml";
            try
            {
                if (storage.DirectoryExists("Celligraphy") && storage.FileExists(filename))
                {
                    storage.DeleteFile(filename);
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
    }

}
